define(["require", "exports"], function(require, exports) {
    (function (Utilities) {
        var Dependency = (function () {
            function Dependency() {
                this.AsyncObservers = new Array();
                this.asyncObservable = null;
            }
            Dependency.prototype.Resolve = function () {
                alert("DependencyExecuteNotOverwrittenException");
            };

            // Can be used in 2 ways i.e. as Setter-Method with argument and as Getter-Method without argument
            Dependency.prototype.AsyncObservable = function (observable) {
                if (typeof observable === "undefined") { observable = null; }
                if (observable != null) {
                    this.asyncObservable = observable;
                    this.Resolved();
                    return null;
                }
                if (this.asyncObservable == null) {
                    alert("AsyncObservableNotInitializedException");
                }
                return this.asyncObservable;
            };

            Dependency.prototype.Resolved = function () {
                jQuery.each(this.AsyncObservers, function () {
                    if (this.MoveNext()) {
                        this.ResolveCurrent();
                    }
                });
            };

            Dependency.prototype.RegisterAsyncObserver = function (observer) {
                this.AsyncObservers.push(observer);
            };
            return Dependency;
        })();
        Utilities.Dependency = Dependency;

        // Base class for an application that needs to make a number of inter-dependent
        // asynchronous calls to a remote server
        var AsyncObserver = (function () {
            function AsyncObserver() {
                this.Dependencies = new Array();
                this.currPos = -1;
            }
            // Adds a new Dependency object to this AsyncObserver's queue
            AsyncObserver.prototype.AddDependency = function (dependency) {
                this.Dependencies.push(dependency);
            };

            // Increments the array index by one and returns true when it's pointing to an existing Dependency
            AsyncObserver.prototype.MoveNext = function () {
                this.currPos++;
                if (this.Dependencies.length > 0 && (this.Dependencies.length - 1 >= this.currPos)) {
                    return true;
                }
                return false;
            };

            // Executes the Resolve method of the current Dependency
            // Usage if(MoveNext()) { ResolveCurrent() }
            AsyncObserver.prototype.ResolveCurrent = function () {
                if (this.currPos >= 0 && (this.Dependencies.length - 1 >= this.currPos)) {
                    this.Dependencies[this.currPos].Resolve();
                } else {
                    alert("ArrayIndexOutOfBoundsException");
                }
            };

            AsyncObserver.prototype.ApplyBinding = function (dependency) {
                dependency.RegisterAsyncObserver(this);
                this.AddDependency(dependency);
            };
            return AsyncObserver;
        })();
        Utilities.AsyncObserver = AsyncObserver;
    })(exports.Utilities || (exports.Utilities = {}));
    var Utilities = exports.Utilities;
});
//# sourceMappingURL=utilities.js.map
